function initDB() {
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    db.transaction(
                function(tx) {
                    // Create the database if it doesn't already exist
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe_date(id integer primary key autoincrement, date LONG)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS user(user_id integer primary key autoincrement, first_name TEXT, last_name TEXT, username TEXT, password TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe_to_date(id integer primary key autoincrement, recipe_id INTEGER, date LONG, portions LONG)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe(recipe_id integer primary key autoincrement, name TEXT, guide TEXT, adder LONG, portion_size INTEGER, cooking_time TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe_to_user(id integer primary key autoincrement, recipe_id INTEGER, user_id INTEGER, review INTEGER, favorite BOOL, own BOOL, at_list BOOL)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe_to_category(id integer primary key autoincrement, recipe_id INTEGER, category_id INTEGER)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS category(category_id integer primary key autoincrement, name TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS recipe_to_material(id integer primary key autoincrement, recipe_id INTEGER, material_id INTEGER, amount LONG, unit TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS material(material_id integer primary key autoincrement, name TEXT, description TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS unit(unit_id integer primary key autoincrement, name TEXT)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS comment(id integer primary key autoincrement, recipe_id INTEGER, comment_text TEXT, added DATE, user_id LONG)');

                }
                )
}

function addRecipeToDB(ingredients, guide, name, user, portion_size, cooking_time){

    var recipeId = -1;
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    db.transaction(
                function(tx) {
                    tx.executeSql('INSERT into recipe(name, guide, adder, portion_size, cooking_time) values(?,?,?,?,?)', [name, guide, user, portion_size, cooking_time]);
                    // Get just added recipe id.
                    var recipeIds = tx.executeSql('SELECT recipe_id FROM recipe');
                    console.log("Recipe ids: " + recipeIds);
                    for(var key in recipeIds){
                        console.log(recipeIds[key]);
                        if(recipeIds[key] > recipeId){
                            recipeId = recipeIds[key];
                        }
                    }
                }
                )
    addIngredientsToDB(ingredients, recipeId);
}

function addIngredientsToDB(ingredients, recipeId){
    console.log("Add ingredients: " + ingredients + ", to " + recipeId);
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    db.transaction(
                function(tx) {
                    for(var i=0; i<ingredients.count; i++){
                        tx.executeSql('INSERT into material(name) values(?)', [ingredients.get(i).ingredient]);
                        var materialId = -1;
                        var materials = tx.executeSql('SELECT material_id FROM material');
                        for(var key in materials){
                            if(materials[key] > materialId){
                                materialId = materials[key];
                            }
                        }
                        var amount = ingredients.get(i).amount;
                        var unit = ingredients.get(i).unit;
                        if(materialId != -1 && recipeId != -1){
                            tx.executeSql('INSERT into recipe_to_material(recipe_id, material_id, amount, unit) values(?,?,?,?)', [recipeId, materialId, amount, unit]);
                        }else{
                            // TODO: error message.
                        }
                    }
                }
                )

}

function clearRecipes(){
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    db.transaction(
                function(tx) {
                    tx.executeSql('DELETE from recipe');
                }
                )
}

function dropTables(){
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    db.transaction(
                function(tx){
                    tx.executeSql('DROP TABLE recipe_date');
                    tx.executeSql('DROP TABLE user');
                    tx.executeSql('DROP TABLE recipe_to_date');
                    tx.executeSql('DROP TABLE recipe');
                    tx.executeSql('DROP TABLE recipe_to_category');
                    tx.executeSql('DROP TABLE recipe_to_user');
                    tx.executeSql('DROP TABLE category');
                    tx.executeSql('DROP TABLE recipe_to_material');
                    tx.executeSql('DROP TABLE material');
                    tx.executeSql('DROP TABLE unit');
                    tx.executeSql('DROP TABLE comment');
                }

                )
}

function recipesForDate(date){

}

function recipesForIngredients(ingredients){

}

function recipesForSearchWord(searchWord){
    console.log("Search word: " + searchWord);
    var db = openDatabaseSync("WhatToEatDB", "1.0", "What To Eat Local database", 1000000);
    var recipesList = [];
    db.transaction(
                function(tx) {
                    var recipes = tx.executeSql('SELECT * FROM recipe WHERE name like "%'+searchWord+'%"');
                    for(var i=0; i<recipes.rows.length; i++){
                        var recipeDetails = [];
                        console.log(recipes.rows.item(i).name);
                        recipeDetails["name"] = recipes.rows.item(i).name;
                        recipeDetails["guide"] = recipes.rows.item(i).guide;
                        recipeDetails["adder"] = recipes.rows.item(i).adder;
                        recipeDetails["portion_size"] = recipes.rows.item(i).portion_size;
                        recipeDetails["cooking_time"] = recipes.rows.item(i).cooking_time;
                        var recipeId = recipes.rows.item(i).recipe_id;
                        var ingredients = tx.executeSql('SELECT * FROM recipe_to_material WHERE recipe_id like ?', [recipeId]);
                        recipeDetails["ingredients"] = ingredients;
                        recipesList.push(recipeDetails);
                    }
                }
                )
    return recipesList;
}

function recipesForAdvancedSearch(params){

}

function getRandomRecipes(){

}
